package com.stu.service.impl;

import com.stu.Pojo.DePartment;
import com.stu.mapper.IDepartmentMapper;
import com.stu.query.DepartmentQuery;
import com.stu.service.IDeperatmentService;

import com.stu.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
@Service
@Transactional
//标记为业务层

public class IDeperatmentServiceImpl implements IDeperatmentService {
    @Autowired
    //注入 持久层
    private IDepartmentMapper department;

    @Override
    public List<DePartment> selectALL() {

        return department.selectALL();
    }

    @Override
    public DePartment selectByid(Long id) {

        return department.selectByid(id);
    }

    @Override
    public void insert(DePartment D) {
        department.insert(D);
    //设置空的dirpath
        String dirPath="";
        //顶级部门  新增dirtpath(/"他本身");
        if (D.getParent()==null){
            dirPath="/"+D.getId();
        }else {
    DePartment  dePartment1=department.selectByid(D.getParent().getId());
    dirPath=dePartment1.getDirpath()+"/"+D.getId();
            System.out.println("本身的"+D.getId());
        }
        D.setDirpath(dirPath);
        department.update(D);
    }

    @Override
    public void update(DePartment dePartment12) {
        String dirPath="";
        //顶级部门  新增dirtpath(/"他本身");
        if (dePartment12.getParent()==null){
            dirPath="/"+dePartment12.getId();
        }else {
            DePartment  dePartment1=department.selectByid(dePartment12.getParent().getId());
            dirPath=dePartment1.getDirpath()+"/"+dePartment12.getId();

        }
        dePartment12.setDirpath(dirPath);
 department.update(dePartment12);
    }

    @Override
    public void delete(Long id) {
 department.delete(id);
    }

    @Override
    public List<DePartment>SelectByPage(DepartmentQuery dq) {

     return department.SelectByPage(dq);

    }

    @Override
    public PageList<DePartment> QueryByPage(DepartmentQuery query) {
        PageList<DePartment> pageList=new PageList<>();
        List<DePartment> dePartments=department.SelectByPage(query);
        pageList.setRows(dePartments);
        Long Total=department.count(query);
        pageList.setTotal(Total);
        return pageList;
    }

    @Override
    public void batchRemove(List<Long> ids) {
        department.batchRemove(ids);
    }
    @Override
    public  List<DePartment> querytree(){
        List<DePartment> dePartments=department.selectALL();
        //parentid为空就是顶级部门
        ArrayList<DePartment> lable=new ArrayList<>();
       for (DePartment dePartment:dePartments){
           if (dePartment.getParent_id()==null){
               lable.add(dePartment);
           }else {
               for (DePartment dep:dePartments){
              if (dePartment.getParent_id()==dep.getId()){
                  if (dep.getChildren()==null){
                      dep.setChildren(new ArrayList<>());
                  }
                  dep.getChildren().add(dePartment);
              }
               }

           }
       }
    return  lable;

    }

}
